#!/usr/bin/python
from __future__ import division
from PyQt4 import QtCore, QtGui
from sshelper3ui import *
import random
import sys

class SSMain(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(SSMain, self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.lineEdit1.textChanged.connect(self.parseInput)
        self.ui.radio_2bb.toggled.connect(self.parseInput)
        self.ui.radio_2_5bb.toggled.connect(self.parseInput)
        self.ui.radio_3bb.toggled.connect(self.parseInput)
      
        
    def showImage(self, image):
        pix = QtGui.QPixmap(image)
        self.ui.handMatrix.setPixmap(pix)
      
    #Converts 'num' to the closest number 'nearest'  
    def convert(self, num, nearest):
        y = round(num/nearest) * nearest
        ys = str(y)
        if (ys[len(ys)-1] == '0'):
            return int(float(ys))
        else:
            return y
        
    def getDefaultStackSize(self, raiseSize):
        if raiseSize == 3:
            return 30
        
        return 20
    
    #Get the correct stoxev image given various parameters
    def getDataImage(self, r, c, s, raiseSize):
        imageFile = "data//{0}bb//R{1}C{2}S{3}.png".format(raiseSize,r,c,s)
        return imageFile
        
    #Parse the user input and return correct stoxev image
    def parseInput(self):
        inText = self.ui.lineEdit1.text()
        raiseSize = 0
        
        if self.ui.radio_2bb.isChecked():
            raiseSize = 2
        elif self.ui.radio_2_5bb.isChecked():
            raiseSize = 2.5
        else:
            raiseSize = 3
            
        s=0
            
        if (len(inText) == 4 or len(inText) == 5):
            s = self.getDefaultStackSize(raiseSize)
          
        elif len(inText) == 6:
            s = int(inText[4:])
          
        else:
            return 0
          
        r = int(inText[:2])
        f = int(inText[2:4])
 
        c = r * (100 - f) / 100
        
        #No data exists with really low call percentages
        if c < 6:
            c = 6
            
        #No data exists with really high raise percentages
        if r > 50:
            r =50
          
        #Convert call, raise and stacks to the nearest even number
        c = self.convert(c, 2)
        r = self.convert(r, 2)
        s = self.convert(s, 2)
        
        #Show the call raise and stack size in UI
        self.ui.lineEdit_call.setText(str(c))
        self.ui.lineEdit_raise.setText(str(r))
        self.ui.lineEdit_stack.setText(str(s))
        
        dataimg = self.getDataImage(r, c, s, raiseSize)
        
        self.showImage(dataimg)
        
        if len(inText) == 6:
            self.ui.lineEdit1.clear()
            
 

        
def main():
    app = QtGui.QApplication(sys.argv)
    MainWindow = SSMain()
    MainWindow.show()
    
    sys.exit(app.exec_())
    
        
main()
        
    